const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const app = getApp();
const apiUrl = app.globalData.hostUrl;

//统一http get 请求
var wGet = function (url, params) {
  return new Promise(function (resolve, reject) {
    // 调研app.js》requestGet，成功-返回，失败刷新token
    app.requestGet(url, params).then(function (value) {
      resolve(value);   //成功-返回
    }, function (value) {
      //失败刷新token
      app.updateAccessToken().then(function (value1) {
        //刷新token后，重新请求app.js》requestGet并返回
        app.requestGet(url, params).then(function (value2) {
          resolve(value2);
        })
      })
      // failure
    })
  });
}

//统一http post 请求
var wPost = function (url, params) {
  return new Promise(function (resolve, reject) {
    app.requestPost(url, params).then(function (value) {
      resolve(value);
    }, function (value) {
      app.updateAccessToken().then(function (value1) {
        app.requestPost(url, params).then(function (value2) {
          resolve(value2);
        })
      })
      // failure
    })
  });
}

// 防止按钮重复点击
var buttonClicked=function buttonClicked(self) {
  self.setData({
    buttonClicked: true
  })
  // setTimeout(function () {
  //   self.setData({
  //     buttonClicked: false
  //   })
  // }, 500)
}


module.exports = {
  formatTime: formatTime,
  wGet: wGet,
  wPost: wPost,
  buttonClicked: buttonClicked
}
